﻿using Castle.Core.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Yto.Common.Dependency;

namespace Yto.Common.logging
{
    public static class LogHelper
    {
        public static ILogger Logger { get; private set; }

        static LogHelper()
        {
            Logger = CtnManager.Instance.IsRegisted<ILoggerFactory>() ?
                CtnManager.Instance.Resolver<ILoggerFactory>().Create(typeof(LogHelper))
                : NullLogger.Instance;
        }

        public static void LogWithException(Exception ex)
        {
            LogWithException(Logger, ex);
        }

        private static void LogWithException(ILogger logger, Exception ex)
        {
            var logLevel = (ex as IWithLogLevel)?.LogLevel ?? LogLevel.Error;

            logger.Log(logLevel, ex.Message, ex);
        }
    }
}
